package husc.se.ndung.soiphubaimobile;

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SettingActivity extends ActionBarActivity implements View.OnClickListener {
    Button btnSave, btnCancel;
    EditText txtIP, txtPort;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        btnSave = (Button)findViewById(R.id.btnSave);
        btnCancel = (Button)findViewById(R.id.btnCancel);
        txtIP = (EditText)findViewById(R.id.txtIP);
        txtPort = (EditText)findViewById(R.id.txtPort);
        btnSave.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        SharedPreferences share = getSharedPreferences("settingData", MODE_PRIVATE);
        String ip = share.getString("IP", "http://spb.somee.com");
        String port = share.getString("Port", "80");
        txtIP.setText(ip);
        txtPort.setText(port);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_setting, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()== btnCancel.getId())
            this.finish();
        else if (v.getId()== btnSave.getId())
        {
            /*SharedPreferences sharedPref = getSharedPreferences("settingData", this.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("IP",txtIP.getText().toString());
            editor.putString("Port", txtPort.getText().toString());
            editor.commit();*/
            Utils.setKeyValue(this,"IP", txtIP.getText().toString());
            Utils.setKeyValue(this,"Port", txtPort.getText().toString());
            setResult(1);
            this.finish();
            //Toast.makeText(this, getString(R.string.save_success), Toast.LENGTH_LONG).show();
        }
    }
}

